package com.heima.common.aliyun;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.Client;
import com.aliyun.green20220302.models.ImageModerationRequest;
import com.aliyun.green20220302.models.ImageModerationResponse;
import com.aliyun.green20220302.models.ImageModerationResponseBody;
import com.aliyun.green20220302.models.ImageModerationResponseBody.ImageModerationResponseBodyData;
import com.aliyun.teautil.models.RuntimeOptions;

import lombok.Getter;
import lombok.Setter;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Getter
@Setter
@Component
public class GreenImageScan {

    @Resource(name = "aliyunClient")
    private Client client;

    public Map imageScan(List<String> imageList) throws Exception {
        // 创建返回参数
        Map<String, String> map = new HashMap<>();

        // 创建RuntimeObject实例并设置运行参数
        RuntimeOptions runtime = new RuntimeOptions();

        for (String imageUrl : imageList) {
            // 检测参数构造。
            Map<String, String> serviceParameters = new HashMap<>();
            //公网可访问的URL。
            serviceParameters.put("imageUrl", imageUrl);
            //待检测数据唯一标识
            serviceParameters.put("dataId", UUID.randomUUID().toString());

            ImageModerationRequest request = new ImageModerationRequest();
            // 图片检测service：内容安全控制台图片增强版规则配置的serviceCode，示例：baselineCheck
            // 支持service请参考：https://help.aliyun.com/document_detail/467826.html?0#p-23b-o19-gff
            request.setService("baselineCheck");
            request.setServiceParameters(JSON.toJSONString(serviceParameters));

            ImageModerationResponse response = null;
            try {
                response = client.imageModerationWithOptions(request, runtime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                // 打印检测结果。
                if (response != null) {
                    if (response.getStatusCode() == 200) {
                        ImageModerationResponseBody body = response.getBody();
                        System.out.println(JSON.toJSONString(body));
                        if (body.getCode() == 200) {
                            ImageModerationResponseBodyData data = body.getData();
                            if (data != null) {
                                String riskLevel = data.getRiskLevel();
                                if (riskLevel != null && ("high".equals(riskLevel) || "medium".equals(riskLevel))) {
                                    map.put("riskLevel", riskLevel);
                                    map.put("message", "no");
                                    return map;
                                }
                            }
                        } else {
                            System.out.println("image moderation not success. code:" + body.getCode());
                            return null;
                        }
                    } else {
                        System.out.println("response not success. status:" + response.getStatusCode());
                        return null;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        map.put("message", "ok");
        return map;
    }
}